/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fork.modbox;

import fork.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import tal.drivechain.chain.Chain;
import tal.drivechain.chain.ChainNode;
import tal.drivechain.gui.ForkScrollBarUI;



/**
 *
 * @author eisental
 */
public class ModboxGUI extends JPanel {

    private Modbox owner;
    private NodeGraph nodeGraph;
    private ChainPathPanel parentsPanel;
    private VariablePopup varPopup;
    private ChildrenPanel childrenPanel;
    private JScrollPane scroll;

    public ModboxGUI(Modbox owner, Font font) {
        this.owner = owner;
        setFont(font);
    }

    public void init(Color color, int width, int height) {
        UIManager.put("ScrollBar.width", new Integer(8));
        this.setLayout(new GridBagLayout());
        this.setBackground(Color.BLACK);
        parentsPanel = new ChainPathPanel(owner);
        childrenPanel = new ChildrenPanel(owner);
        varPopup = new VariablePopup(owner);
        varPopup.setFont(owner.getFont());
        
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0; c.gridy = 0; c.anchor = GridBagConstraints.PAGE_START; c.fill = GridBagConstraints.BOTH;
        this.add(parentsPanel, c);

        nodeGraph = new NodeGraph(owner, color, getFont());
        JPanel blackPanel = new JPanel();
        blackPanel.setBackground(Color.BLACK);
        scroll = new JScrollPane(nodeGraph);
        scroll.setCorner(JScrollPane.LOWER_TRAILING_CORNER, blackPanel);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        c = new GridBagConstraints();
        c.gridx = 0; c.gridy = 1; c.weighty = 1; c.weightx = 1; c.fill = GridBagConstraints.BOTH;
        this.add(scroll, c);

        c = new GridBagConstraints();
        c.gridx = 0; c.gridy = 2; c.weightx = 1; c.fill = GridBagConstraints.BOTH;
        this.add(childrenPanel, c);

        this.setColor(color);
        this.validate();
    }

    public NodeGraph getGraph() { return nodeGraph; }
    public ChainPathPanel getChainPathPanel() { return parentsPanel; }

    public void setColor(Color color) {
        if (nodeGraph!=null) {
            nodeGraph.setColor(color);
            JScrollPane scroll = (JScrollPane)nodeGraph.getParent().getParent();
            scroll.getHorizontalScrollBar().setUI(new ForkScrollBarUI(color, color.darker()));
            scroll.getVerticalScrollBar().setUI(new ForkScrollBarUI(color, color.darker()));
            scroll.setBorder(new LineBorder(color.darker(), 1));
            scroll.setViewportBorder(new LineBorder(color.darker(), 1));

        }
        if (parentsPanel!=null)
            parentsPanel.setColor(color);
        if (childrenPanel!=null)
            childrenPanel.setColor(color);
        if (varPopup!=null)
            varPopup.setColor(color);
    }

    @Override
    public void setFont(Font font) {
        super.setFont(font);
        if (parentsPanel!=null)
            parentsPanel.setFont(font);
        if (nodeGraph!=null)
            nodeGraph.setFont(font);
        if (varPopup!=null)
            varPopup.setFont(font);
    }

    public Color getColor() {
        return nodeGraph.getColor();
    }

    public void addNode(ChainNode node) {
        nodeGraph.addNodeCube(node);
        if (node instanceof Chain)
            childrenPanel.refresh();
    }

    public void removeNode(ChainNode node) {
        nodeGraph.removeNodeCube(node);

        owner.sendSelectionChange();
        node.destroy();
        System.gc();
        if (node instanceof Chain) childrenPanel.refresh();
    }

    public ChildrenPanel getChildrenPanel() {
        return childrenPanel;
    }

    @Override
    public void addNotify() {
        super.addNotify();
        owner.setActualBoxChain(owner.getActualBoxChain());
    }

    void showNodePopup(ChainNode chainNode, Point locationOnScreen) {
        NodePopup n;
        if (this.getGraph().getSelectedCubes().size()>1) {
            n = getGraph().getMultiNodePopup();
        } else {
            n = getGraph().getSingleNodePopup();
        }

        n.updateSelection(getGraph().getSelectedCubes());
        n.setLocation(locationOnScreen);
        n.setVisible(true);
    }

    public JScrollPane getScroll() {
        return scroll;
    }

    void showVariablesPopup(Point locationOnScreen) {
        varPopup.setLocation(locationOnScreen);
        varPopup.setVisible(true);
    }

    public VariablePopup getVariablePopup() {
        return varPopup;
    }
}




